package com.xm.xmds1705083.service.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xm.xm.dto.ResultDto;
import com.xm.xm.dto.SearchDto;
import com.xm.xm.util.DateUtils;
import com.xm.xm.util.ErrorCode;
import com.xm.xm.util.ValidateUtils;
import com.xm.xmds1705083.service.dto.DuoduoSpaceCollectionDto;
import com.xm.xmds1705083.service.dto.DuoduoSpaceExchangeDto;
import com.xm.xmds1705083.service.mapper.DuoduoAreaMapper;
import com.xm.xmds1705083.service.mapper.DuoduoSpaceCollectionMapper;
import com.xm.xmds1705083.service.model.DuoduoSpaceCollection;
import com.xm.xmds1705083.service.model.DuoduoSpaceExchange;
import com.xm.xmds1705083.service.service.DuoduoSpaceCollectionService;
import com.xm.xmds1705083.service.util.DateUtil;
import com.xm.xmds1705083.service.util.GlobalError;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 杭州新苗网络科技有限公司
 *
 * @author meihf
 * @date 2017/06/14
 * @description
 */
@Service("duoduoSpaceCollectionService")
public class DuoduoSpaceCollectionServiceImpl extends ServiceImpl<DuoduoSpaceCollectionMapper, DuoduoSpaceCollection> implements DuoduoSpaceCollectionService {

    @Autowired
    DuoduoSpaceCollectionMapper duoduoSpaceCollectionMapper;

    @Autowired
    DuoduoAreaMapper duoduoAreaMapper;

    @Override
    public ResultDto save(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        JSONObject filed = searchDto.getFiled();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            if (!filed.has("id")) {
                resultDto.setMsg(ErrorCode.E10011);
                resultDto.setCode(ErrorCode.C10011);
            } else {
                long id = filed.getLong("id");
                //先查有没有收藏过，如果收藏过则不再重复收藏
                EntityWrapper entityWrapper = new EntityWrapper();
                entityWrapper.where(" spaceExchangeId={0} and userid={1}",id,searchDto.getAppUserId());
                int count = duoduoSpaceCollectionMapper.selectCount(entityWrapper);
                if (ValidateUtils.isGreaterThan0(count)){
                    resultDto.setMsg(GlobalError.E11023);
                    resultDto.setCode(GlobalError.C11023);
                    return resultDto;
                }else {
                    DuoduoSpaceCollection duoduoSpaceCollection = new DuoduoSpaceCollection();
                    duoduoSpaceCollection.setSpaceExchangeId((int) id);
                    duoduoSpaceCollection.setCollectionTime(searchDto.getTime().intValue());
                    duoduoSpaceCollection.setUserId(searchDto.getAppUserId().intValue());
                    int result = duoduoSpaceCollectionMapper.insert(duoduoSpaceCollection);
                    if (ValidateUtils.isGreaterThan0(result)) {
                        resultDto.setMsg("收藏成功");
                    } else {
                        resultDto.setMsg(ErrorCode.E11004);
                        resultDto.setCode(ErrorCode.C11004);
                    }
                }
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

    @Override
    public ResultDto findMyCollections(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            List<DuoduoSpaceExchangeDto> duoduoSpaceCollectionDtos = duoduoSpaceCollectionMapper.findMyCollections(searchDto);
            if (duoduoSpaceCollectionDtos.size() == 0) {
                resultDto.setCode(ErrorCode.C11001);
                resultDto.setMsg(ErrorCode.E11001);
                return resultDto;
            } else {
                JSONArray collectionsArr = JSONArray.fromObject(duoduoSpaceCollectionDtos);
                collectionsArr.forEach((coll)->{
                    String endTime = DateUtil.timeStamp2Date(((JSONObject)coll).getLong("endTime"), DateUtils.PATTERN_DATE+" HH");
                    ((JSONObject) coll).put("endTime",endTime);

                    String startTime = DateUtil.timeStamp2Date(((JSONObject)coll).getLong("startTime"), DateUtils.PATTERN_DATE+" HH");
                    ((JSONObject) coll).put("startTime",startTime);

                    ((JSONObject)coll).put("area",duoduoAreaMapper.selectById(((JSONObject) coll).getLong("area")).getArea());
                    ((JSONObject)coll).put("province",duoduoAreaMapper.selectById(((JSONObject) coll).getLong("province")).getArea());
                    ((JSONObject)coll).put("county",duoduoAreaMapper.selectById(((JSONObject) coll).getLong("county")).getArea());
                });
                resultDto.setData(collectionsArr);
                resultDto.setMsg("查询成功！");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

    public static void main(String args[]) {
        String startTime = DateUtil.timeStamp2Date(1498615560, DateUtils.PATTERN_DATE+" HH");
        System.out.println(startTime);
    }
}

